package com.dongdong.service.impl;

import cn.hutool.core.util.StrUtil;
import com.dongdong.mapper.TbSpecificationMapper;
import com.dongdong.model.TbSpecification;
import com.dongdong.model.TbSpecificationExample;
import com.dongdong.result.PageBean;
import com.dongdong.result.PageResult;
import com.dongdong.service.SpecOptionService;
import com.dongdong.service.SpecService;
import com.dongdong.vo.SpecOptionVO;
import com.dongdong.vo.SpecVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SpecServiceImpl implements SpecService {

    @Autowired
    private TbSpecificationMapper specificationMapper;

    @Autowired
    private SpecOptionService specOptionService;

    /**
     * 分页模糊查询
     * @param bean
     * @return
     */
    @Override
    public PageResult<SpecVO> FindSpecList(PageBean<SpecVO> bean) {
        TbSpecificationExample specificationExample = new TbSpecificationExample();
        TbSpecificationExample.Criteria criteria = specificationExample.createCriteria();
        if (!StrUtil.isEmpty(bean.getParms().getSpecName())){
            criteria.andSpecNameLike("%" + bean.getParms().getSpecName() + "%");
        }
        PageHelper.startPage(bean.getPageNumber(),bean.getPageSize());
        List<TbSpecification> tbSpecifications = specificationMapper.selectByExample(specificationExample);
        PageInfo<TbSpecification> info = new PageInfo<>(tbSpecifications);
        List<SpecVO> vos = info.getList().stream().map((x) -> {
            SpecVO specVO = new SpecVO();
            BeanUtils.copyProperties(x, specVO);
            return specVO;
        }).collect(Collectors.toList());
        return new PageResult<>(info.getPageNum(),info.getPageSize(),info.getTotal(),vos);
    }

    /**
     * 根据ID回显规格
     * @param id
     * @return
     */
    @Override
    public SpecVO selectSpecById(Long id) {
        TbSpecification tbSpecification = specificationMapper.selectByPrimaryKey(id);
        SpecVO specVO = new SpecVO();
        BeanUtils.copyProperties(tbSpecification,specVO);
        //根据ID查询规格选项表
        List<SpecOptionVO> specOptionVOS = specOptionService.SelectSpecOptionBySpecId(id);
        specVO.setSpecOptionVO(specOptionVOS);
        return specVO;
    }

    /**
     * 增加规格
     * @param specVO
     */
    @Override
    @Transactional
    public void InsertSpec(SpecVO specVO) {
        TbSpecification tbSpecification = new TbSpecification();
        BeanUtils.copyProperties(specVO,tbSpecification);
        specificationMapper.insertSelective(tbSpecification);
        //获取规格表ID
        Long id = tbSpecification.getId();
        //获取规格选项数据
        List<SpecOptionVO> specOptionVO = specVO.getSpecOptionVO();
        //循环规格选项
        for (SpecOptionVO optionVO : specOptionVO) {
            //将规格IDset进规格选项
            optionVO.setSpecId(id);
        }
        //增加规格选项
        specOptionService.InsertSpecOption(specOptionVO);
    }

    /**
     * 修改规格
     * @param specVO
     */
    @Override
    @Transactional
    public void UpdateSpec(SpecVO specVO) {
        TbSpecification tbSpecification = new TbSpecification();
        BeanUtils.copyProperties(specVO,tbSpecification);
        specificationMapper.updateByPrimaryKeySelective(tbSpecification);
        //获取规格选项数据
        List<SpecOptionVO> specOptionVO = specVO.getSpecOptionVO();
        //循环规格选项
        for (SpecOptionVO optionVO : specOptionVO) {
            //将规格IDset进规格选项
            optionVO.setSpecId(tbSpecification.getId());
        }
        //先删除该ID下所有规格选项数据
        specOptionService.DeleteSpecOptionBySpecId(tbSpecification.getId());
        //在增加规格选项
        specOptionService.InsertSpecOption(specOptionVO);
    }

    /**
     * 删除规格
     * @param id
     */
    @Override
    public void DeleteSpecById(Long id) {
        //先根据ID删除规格选项
        specOptionService.DeleteSpecOptionBySpecId(id);
        //再删除规格
        specificationMapper.deleteByPrimaryKey(id);
    }

    /**
     * 查询全部规格
     * @return
     */
    @Override
    public List<TbSpecification> FindBrandAll() {
        return specificationMapper.selectByExample(null);
    }
}
